﻿using FastCloud.Base;
using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastCloud.DMP.DB;
using FastCloud.DMP.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.DMP
{
    [Route("frame/dmp/codeitem")]
    public class CodeItemController : BaseController
    {
        [AutoWired]
        private DB_CodeItem DB_CodeItem;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/DMP/CodeItem/CodeItem_List.cshtml");
        }
        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/DMP/CodeItem/CodeItem_Add.cshtml");
        }
        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/DMP/CodeItem/CodeItem_Edit.cshtml");
        }
        [Route("moreleveladd")]
        public IActionResult MoreLevelAdd()
        {
            return View("~/Views/DMP/CodeItem/CodeItemMoreLevel_Add.cshtml");
        }
        [Route("moreleveledit")]
        public IActionResult MoreLevelEdit()
        {
            return View("~/Views/DMP/CodeItem/CodeItemMoreLevel_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string MainGuid = ValueConvert.ConvertToString(GetFormParam("MainGuid"));
            string ItemName = ValueConvert.ConvertToString(GetFormParam("ItemName"));
            string ItemValue = ValueConvert.ConvertToString(GetFormParam("ItemValue"));
            return Result.OK(DB_CodeItem.SelectPage(MainGuid, ItemName, ItemValue));
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_CodeItem Entity)
        {
            if (DB_CodeItem.ExistCodeItem(Entity.MainGuid, Entity.ItemValue))
            {
                return Result.Error("已存在相同的值！");
            }
            else
            {
                DB_CodeItem.Insert(Entity);
                return Result.OK();
            }
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_CodeItem Entity)
        {
            if (DB_CodeItem.ExistCodeItem(Entity.MainGuid, Entity.ItemGuid, Entity.ItemValue))
            {
                return Result.Error("已存在相同的值！");
            }
            else
            {
                DB_CodeItem.UpdateByID(Entity);
                return Result.OK();
            }
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string ItemGuid = ValueConvert.ConvertToString(GetFormParam("ItemGuid"));
            return Result.OK(DB_CodeItem.SelectOne(ItemGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            DB_CodeItem.DeleteBatch(KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
            return Result.OK();
        }

        [Route("tree"), HttpGet]
        public IActionResult Tree()
        {
            string MainGuid = ValueConvert.ConvertToString(GetFormParam("MainGuid"));
            List<Frame_CodeItem> CodeItemList = DB_CodeItem.SelectCodeItemListByMainGuid(MainGuid);
            CodeItemList.Sort((x, y) => -x.SortNum.CompareTo(y.SortNum));
            List<TreeNode> NodeList = new List<TreeNode>();
            Frame_CodeItem Root = new Frame_CodeItem();
            Root.ItemGuid = "Top";
            Root.ItemName = "顶级代码项";
            Root.ParentItemGuid = "";
            CodeItemList.Add(Root);
            foreach (Frame_CodeItem item in CodeItemList)
            {
                TreeNode node = new TreeNode();
                node.ID = item.ItemGuid;
                node.Name = item.ItemName;
                node.PID = item.ParentItemGuid;
                if (item.ItemGuid == Root.ItemGuid)
                {
                    node.IsTop = true;
                    node.Open = true;
                }
                NodeList.Add(node);
            }
            return Result.OK(CloudUtil.BuildTree(NodeList));
        }
    }
}
